<?php
class ControllerAccountTree extends Controller {
	private $error = array();

	public function index() {

		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('account/user', '', 'SSL');

			$this->redirect($this->url->link('account/login', '', 'SSL'));
		}

		$this->language->load('account/edit');
	
		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('account/customer');
		
		if($this->customer->getId()){
			$this->load->model('account/customer');
			
			$cus_infos= array();
			
			$this->getChildCustomer($this->customer->getId());
			
			$this->data['back'] = $this->url->link('account/account', '', 'SSL');

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/tree.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/account/tree.tpl';
			} else {
				$this->template = 'default/template/account/tree.tpl';
			}
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'	
			);
							
			$this->response->setOutput($this->render());	
		}
	}
	public function getChildCustomer($customer_id) {
		$cus_infos = $this->model_account_customer->getAllChildCustomers($this->customer->getId());

		foreach($cus_infos as $cus_info){
			$cus_info['price'] = $this->currency->format($cus_info['price']);
			$cus_info['price2'] = $this->currency->format($cus_info['price2']);
			$cus_info['date_added'] = date('d/m/Y', strtotime($cus_info['date_added']));
			$this->data['customers'][] = $cus_info;
		}
	}
	
	public function GetCustomer() {
		$json = array();
		$this->load->model('account/customer');
		$customer_nhan = $this->model_account_customer->getCustomerById($this->request->post['user_nhan']);
		$customer_nhan['price'] = $this->currency->format($customer_nhan['price']);
		$customer_nhan['price2'] = $this->currency->format($customer_nhan['price2']);
		$customer_nhan['date_added'] = date('d/m/Y', strtotime($customer_nhan['date_added']));
		if(!$customer_nhan){
			$json['error'] = 'Mã khách hàng không tồn tại trong hệ thống';
		}else{
			$json['success'] = $customer_nhan;
		}
		$this->response->setOutput(json_encode($json));		
  	}
	
	public function getChildCustomersTree() {
		$json = array();
		$this->load->model('account/customer');
		$customers = $this->model_account_customer->getChildCustomersTree($this->request->post['cus_id']);
		 $customersTree=array();
		foreach($customers as $cus_info){
			$cus_info['price'] = $this->currency->format($cus_info['price']);
			$cus_info['price2'] = $this->currency->format($cus_info['price2']);
			$cus_info['date_added'] = date('d/m/Y', strtotime($cus_info['date_added']));
			$customersTree[] = $cus_info;
		} 
		if(!$customersTree){
			$json['error'] = 'Đã hết danh sách';
		}else{
			$json['success'] = $customersTree;
		}
		$this->response->setOutput(json_encode($json));		
  	}  
}
?>